package com.ljx.qqpro.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ljx.qqpro.common.utils.UserHolder;
import com.ljx.qqpro.dto.chat.ChatCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljx.qqpro.entity.ChatEntity;
import com.ljx.qqpro.service.ChatService;
import com.ljx.qqpro.common.utils.PageUtil;
import com.ljx.qqpro.common.utils.Result;



/**
 * 聊天表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-18 17:28:14
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = chatService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		ChatEntity chat = chatService.getById(id);

        return Result.success(chat, "chat");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody ChatEntity chat){
		chatService.save(chat);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ChatEntity chat){
		chatService.updateById(chat);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		chatService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }


    @GetMapping("/getUserChat")
    public Result<List<ChatCell>> getUserChat(){
        Long userId = UserHolder.getUser().getId();
        List<ChatCell> userChat = chatService.getUserChat(userId);

        return Result.success(userChat, "获取用户聊天列表成功");
    }
}
